package com.ruoyi.asset.service.impl;

import java.util.List;

import com.ruoyi.asset.domain.AssetImg;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inventory.domain.InventoryByIdDetail;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AssetDetailMapper;
import com.ruoyi.asset.domain.AssetDetail;
import com.ruoyi.asset.service.IAssetDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 资产信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-10-23
 */
@Service
public class AssetDetailServiceImpl implements IAssetDetailService {
    @Autowired
    private AssetDetailMapper assetDetailMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询资产信息
     * 
     * @param id 资产信息ID
     * @return 资产信息
     */
    @Override
    public AssetDetail selectAssetDetailById(String id)
    {
        return assetDetailMapper.selectAssetDetailById(id);
    }

    /**
     * 查询资产信息列表
     * 
     * @param assetDetail 资产信息
     * @return 资产信息
     */
    @Override
    public List<AssetDetail> selectAssetDetailList(AssetDetail assetDetail)
    {
        return assetDetailMapper.selectAssetDetailList(assetDetail);
    }

    /**
     * 新增资产信息
     * 
     * @param assetDetail 资产信息
     * @return 结果
     */
    @Override
    public int insertAssetDetail(AssetDetail assetDetail)
    {
        assetDetail.setCreateTime(DateUtils.getNowDate());
        return assetDetailMapper.insertAssetDetail(assetDetail);
    }

    /**
     * 修改资产信息
     * 
     * @param assetDetail 资产信息
     * @return 结果
     */
    @Override
    public int updateAssetDetail(AssetDetail assetDetail)
    {
        assetDetail.setUpdateTime(DateUtils.getNowDate());
        return assetDetailMapper.updateAssetDetail(assetDetail);
    }

    /**
     * 删除资产信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAssetDetailByIds(String ids)
    {
        return assetDetailMapper.deleteAssetDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除资产信息信息
     * 
     * @param id 资产信息ID
     * @return 结果
     */
    @Override
    public int deleteAssetDetailById(String id)
    {
        return assetDetailMapper.deleteAssetDetailById(id);
    }

    /**
     * 添加资产图片保存
     * @param assetImg
     * @return
     */
    @Override
    public int addImgSave(AssetImg assetImg) {
        return assetDetailMapper.addImgSave(assetImg);
    }

    /**
     *  查询资产图片
     * @param id 资产id
     * @return
     */
    @Override
    public List<String> findImg(String id) {
        return assetDetailMapper.findImg(id);
    }

    /**
     * 导入资产信息数据
     *
     * @param assetDetailList 集团信息数据列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importAssetDetail(List<AssetDetail> assetDetailList, String operName) {
        if (StringUtils.isNull(assetDetailList) || assetDetailList.size() == 0)
        {
            throw new BusinessException("导入资产信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AssetDetail assetDetail :assetDetailList) {
            try {
                if (StringUtils.isBlank(assetDetail.getAssetName())){
                    failureNum++;
                    failureMsg.append("<br/>资产名称不能为空");
                    continue;
                }
                if (StringUtils.isBlank(assetDetail.getAssetType())){
                    failureNum++;
                    failureMsg.append("<br/>资产类型不能为空");
                    continue;
                }
                if (StringUtils.isBlank(assetDetail.getConsumeType())){
                    failureNum++;
                    failureMsg.append("<br/>消耗类型不能为空");
                    continue;
                }
                if (assetDetail.getAmount()==null||assetDetail.getAmount()==0){
                    failureNum++;
                    failureMsg.append("<br/>数量错误");
                    continue;
                }
                if (StringUtils.isBlank(assetDetail.getManageDept())){
                    failureNum++;
                    failureMsg.append("<br/>管理部门不能为空");
                    continue;
                }
                if (StringUtils.isBlank(assetDetail.getDeptId())){
                    failureNum++;
                    failureMsg.append("<br/>使用部门不能为空");
                    continue;
                }
                SysDictData sysDictData=new SysDictData();
                sysDictData.setDictType("asset_Type");
                sysDictData.setDictLabel(assetDetail.getAssetType());
                List<SysDictData> sysDictDataList=sysDictDataMapper.selectDictDataList(sysDictData);
                if (CollectionUtils.isEmpty(sysDictDataList)){
                    failureNum++;
                    failureMsg.append("<br/>资产类型不匹配");
                    continue;
                }
                assetDetail.setAssetType(sysDictDataList.get(0).getDictValue());

                sysDictData.setDictType("consume_type");
                sysDictData.setDictLabel(assetDetail.getConsumeType());
                List<SysDictData> consumeTypeList=sysDictDataMapper.selectDictDataList(sysDictData);
                if (CollectionUtils.isEmpty(consumeTypeList)){
                    failureNum++;
                    failureMsg.append("<br/>消耗类型不匹配");
                    continue;
                }
                assetDetail.setAssetType(sysDictDataList.get(0).getDictValue());

                SysDept sysDept=new SysDept();
                sysDept.setDeptName(assetDetail.getManageDept());
                sysDept.setParentId(101L);
                List<SysDept> sysDepts=sysDeptMapper.selectDeptList(sysDept);
                if (!CollectionUtils.isEmpty(sysDepts)){
                    assetDetail.setManageDept(sysDepts.get(0).getDeptId().toString());
                }else{
                    failureNum++;
                    failureMsg.append("<br/>管理部门不匹配");
                    continue;
                }
                sysDept.setDeptName(assetDetail.getDeptId());
                sysDepts=sysDeptMapper.selectDeptList(sysDept);
                if (!CollectionUtils.isEmpty(sysDepts)){
                    assetDetail.setDeptId(sysDepts.get(0).getDeptId().toString());
                }else{
                    failureNum++;
                    failureMsg.append("<br/>使用部门不匹配");
                    continue;
                }
                if (StringUtils.isNotBlank(assetDetail.getManageUser())&&StringUtils.isNotBlank(assetDetail.getManageDept())){
                    boolean flagManger=true;
                    SysUser sysUser=new SysUser();
                    sysUser.setDeptId(Long.parseLong(assetDetail.getManageDept()));
                    sysUser.setUserName(assetDetail.getManageUser());
                    List<SysUser> sysUsers=sysUserMapper.selectUserList(sysUser);
                    if (!CollectionUtils.isEmpty(sysUsers)){
                        flagManger=false;
                        assetDetail.setUseUser(sysUsers.get(0).getUserId().toString());
                    }
                    if (flagManger){
                        failureNum++;
                        failureMsg.append("<br/>管理人不匹配");
                        continue;
                    }
                }
                if (StringUtils.isNotBlank(assetDetail.getUseUser())&&StringUtils.isNotBlank(assetDetail.getDeptId())){
                    boolean flag=true;
                    SysUser sysUser=new SysUser();
                    sysUser.setDeptId(Long.parseLong(assetDetail.getDeptId()));
                    sysUser.setUserName(assetDetail.getUseUser());
                    List<SysUser> sysUsers=sysUserMapper.selectUserList(sysUser);
                    if (!CollectionUtils.isEmpty(sysUsers)){
                        flag=false;
                        assetDetail.setUseUser(sysUsers.get(0).getUserId().toString());
                    }
                    if (flag){
                        failureNum++;
                        failureMsg.append("<br/>使用人不匹配");
                        continue;
                    }
                }
                assetDetail.setCreateBy(operName.toString());
                assetDetail.setPutDate(assetDetail.getUseDate());
                this.insertAssetDetail(assetDetail);
                successNum++;
            }catch (Exception e){
                failureNum++;
                failureMsg.append(assetDetail.getAssetName()+"-"+e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 删除资产图片
     * @param assetImg
     * @return
     */
    @Override
    public int removeImg(AssetImg assetImg) {
        return assetDetailMapper.removeImg(assetImg);
    }

    /**
     * 查询资产图片数量
     * @return
     */
    @Override
    public List<AssetDetail> selectImgCountByAssetId() {
        return assetDetailMapper.selectImgCountByAssetId();
    }
}
